<template>
    <div class="model-preview">

        <ul :loading="loading">
            <li v-for="model in modelList" :key="model.canvas_name">
                <figure>
                    <img v-for="img in model.image" :key="img.url" :src="img.url" :style="img.style" @load="onStatechange(img,$event)" @error="onStatechange(img,$event)">
                </figure>
                <span v-text="model.surface_name"></span>
            </li>
        </ul>

    </div>
</template>
<script>
export default {
    name: "ModelPreview",
    props: {
        pieces: { type: Array, default: () => [] },
        models: { type: Array, default: () => [] },
    },
    data () {
        return {
            modelList: [],
            loading: false,
        };
    },
    watch: {
        pieces () {
            this.getModelList()
        },
        models () {
            this.getModelList()
        },
    },
    methods: {
        getModelList () {

            const piecesObject = Object.fromEntries(this.pieces.map((i) => [i.surface_name, i]));
            this.modelList = this.models.map(model => {
                const preview = {
                    canvas_name: model.canvas_name,
                    surface_name: model.surface_name,
                    image: []
                }
                // 背景图
                model.mask_image && preview.image.push({
                    url: this.$getImageUrl(model.mask_image),
                    style: null,
                });

                // 渲染图
                model.mapping.map(mapper => {
                    const piece = piecesObject[mapper.canvas_name]
                    piece && piece.output_img && preview.image.push({
                        url: this.$getLitimgUrl(piece.output_img),
                        name: mapper.canvas_name,
                        style: {
                            left: 100 * (mapper.x / model.outer_width) + '%',
                            top: 100 * (mapper.y / model.outer_height) + '%',
                            width: 100 * (mapper.width / model.outer_width) + '%',
                            height: 100 * (mapper.height / model.outer_height) + '%',
                        }
                    });
                })
                return preview
            })

            this.loading = this.modelList.length > 0;
            this.$nextTick(() => this.onStatechange())
        },
        onStatechange (img, event) {
            if (this.$el) {
                const elems = this.$el.querySelectorAll('ul img');
                const images = Array.prototype.slice.call(elems);
                const unload = images.filter(img => !img.complete);
                this.loading = unload.length > 0;
            } else {
                this.loading = false;
            }
        }
    },
    created () {
        this.getModelList();
    }
};
</script>
<style lang="scss" scoped>
.model-preview {
    flex-direction: column;
    justify-content: center;
    position: relative;
    background: #f0f0f0;
    border-radius: 8px;
    box-sizing: border-box;
    height: 394px;

    & > p {
        margin-top: 26px;
        text-align: center;

        strong {
            display: block;
            height: 18px;
            font-size: 16px;
            color: #000000;
            line-height: 18px;
        }
        span {
            height: 16px;
            font-size: 14px;
            color: #555555;
            line-height: 16px;
        }
    }

    & > ul {
        margin: 42px 0 0;
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;

        &[loading] {
            background-image: url(@/assets/images/loading.gif);
            background-position: center;
            background-size: 32px 32px;
            background-repeat: no-repeat;
            li {
                opacity: 0;
            }
        }
        li {
            width: 128px;
            height: 256px;
            margin: 0 10px;
            position: relative;
            overflow: hidden;

            figure {
                position: absolute;
                left: 50%;
                top: 0;
                transform: translateX(-50%);
                width: 256px;
                height: 256px;
            }
            img {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
            }

            & > span {
                display: none;
            }
        }
    }
}
</style>
